package org.overtheair.say.ui;

import java.util.ArrayList;
import java.util.List;

import org.openintents.intents.WikitudeARIntent;
import org.openintents.intents.WikitudePOI;
import org.overtheair.say.common.interfaces.ISAYDataObject;
import org.overtheair.say.logic.DataStore;

import android.app.Activity;
import android.app.Application;
import android.content.ActivityNotFoundException;
import android.content.res.Resources;
import android.widget.Toast;

public class WikitudeAPIProvider {

	public static void startARViewBasic(
			Activity activity,
			DataStore dataStore
		)
	{

		// Create the basic intent
		WikitudeARIntent intent = prepareIntent(activity.getApplication(), dataStore, activity);

		// And launch the intent
		try {
			intent.startIntent(activity);
		} catch (ActivityNotFoundException e) {
			WikitudeARIntent.handleWikitudeNotFound(activity);
			Toast.makeText(activity.getApplication(), "Error", Toast.LENGTH_LONG).show();
		}
	}
	
	private static WikitudeARIntent prepareIntent(
			Application application,
			DataStore dataStore, Activity activity
		)
	{
		// create the intent
		WikitudeARIntent intent = new WikitudeARIntent(
				application,
				null,
				null);

		// add the POIs
		addPois(intent, dataStore, activity);
		
		// add one menu item
//		intent.setMenuItem1("My menu item",
//				BasicOpenARDemoActivity.CALLBACK_INTENT);
		intent.setPrintMarkerSubText(false);
		return intent;
	}

	private static void addPois(
			WikitudeARIntent intent,
			DataStore dataStore, Activity activity
		)
	{
		WikitudePOI wkPoi;
		
		List<WikitudePOI> pois = new ArrayList<WikitudePOI>();
		if (dataStore.photosEnabled && null != dataStore.photos)
		{
			for (ISAYDataObject dataObject: dataStore.photos)
			{
				wkPoi = dataObject.toWikitudePOI();
				if (null != wkPoi)
				{
					//DataFlickrPhoto photo = (DataFlickrPhoto) dataObject;
					//Toast.makeText(activity.getApplicationContext(), photo.uri, Toast.LENGTH_LONG).show();
					//Log.d("AAAA", wkPoi.getIconuri());
					pois.add(wkPoi);
				}
			}
		}
		
		if (dataStore.eventsEnabled && null != dataStore.events)
		{
			for (ISAYDataObject dataObject: dataStore.events)
			{
				wkPoi = dataObject.toWikitudePOI();
				if (null != wkPoi) pois.add(wkPoi);
			}
		}
		
		if (dataStore.moviesEnabled &&  null != dataStore.movies)
		{
			for (ISAYDataObject dataObject: dataStore.movies)
			{
				wkPoi = dataObject.toWikitudePOI();
				if (null != wkPoi) pois.add(wkPoi);
			}
		}
		
//		intent.addPOIs(pois);
		
		
		
//		WikitudePOI poi1 = new WikitudePOI(35.683333, 139.766667, 36, "Tokyo",
//		"Tokyo is the capital of Japan.");
//poi1.setLink("http://www.tourism.metro.tokyo.jp/");
//WikitudePOI poi2 = new WikitudePOI(
//		41.9,
//		12.5,
//		14,
//		"Rome",
//		"Rome is the capital of Italy and the country's largest and most populous city, with over 2.7 million residents.");
//WikitudePOI poi3 = new WikitudePOI(
//		40.716667,
//		-74,
//		1,
//		"New York",
//		"New York is the most populous city in the United States, and the center of the New York metropolitan area.");
//WikitudePOI poi4 = new WikitudePOI(48.208333, 16.373056, 220, "Vienna",
//		"Vienna is the capital of the Republic of Austria.");
////List<WikitudePOI> pois = new ArrayList<WikitudePOI>();
//
//pois.add(poi1);
//pois.add(poi2);
//pois.add(poi3);
//pois.add(poi4);
intent.addPOIs(pois);

//		((BasicOpenARDemoApplication) this.getApplication()).setPois(pois);
	}

	/**
	 * helper-method to add icons to the intent.
	 * 
	 * @param intent
	 *            the intent
	 */
	private void addIcons(
			Resources resources,
			WikitudeARIntent intent
		)
	{
//		ArrayList<WikitudePOI> pois = intent.getPOIs();
//
//		Resources res = getResources();
//		pois.get(0).setIconresource(res.getResourceName(R.drawable.flag_japan));
//		pois.get(1).setIconresource(res.getResourceName(R.drawable.flag_italy));
//		pois.get(2).setIconresource(res.getResourceName(R.drawable.flag_usa));
//		pois.get(3).setIconresource(res.getResourceName(R.drawable.flag_austria));
		//to use this, make sure you have the file present on the sdcard
		//pois.get(3).setIconuri("content://com.IconCP/sdcard/flag_austria.png");
	}
	
	
}
